<?php
namespace app\api\controller;

use app\BaseController;
use app\api\model\BbsModel;
use think\Db;

class Bbsnurse extends BaseController {

	//-------------------------------论坛接口----------------------------------------------------------------------

	/**
	 * 得到论坛所有的分类
	 */
	function allcategory()
	{ 
		$bbsModel = new BbsModel();
		$bbsCategoryList = $bbsModel->findAllCategory();	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsCategoryList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	/**
	 * 得到后台推荐的论坛列表
	 */
	function bbsentityImportantList()
	{
		 
		$bbsModel = new BbsModel();
		$bbsImportantList = $bbsModel->findBBsentityImportantList();	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsImportantList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	
	/**
	 * 搜索论坛
	 */
	function bbsentitySearch()
	{
		$keyWord = $this->getParam("keyWord");
		
		if(empty($keyWord)){
			echo json_encode(array("success"=>0,"code"=>0,"msg"=>"请提供关键词"),true);
			return;
		}
		
		$bbsModel = new BbsModel();
		$bbsImportantList = $bbsModel->bbsentitySearch($keyWord);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsImportantList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	
	/**
	 * 根据论坛类别，得到论坛列表
	 */
	function bbsentityListByCategoryId()
	{
		$categoryId = $this->getParam("categoryId");
		 
		$bbsModel = new BbsModel();
		$bbsEntityList = $bbsModel->findBBsentityListByCategoryId($categoryId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsEntityList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	
	/**
	 * 得到会员关注论坛列表
	 */
	function memberBbsList()
	{
		$toMemberId = $this->getParam("toMemberId");
		
		if(!$this->pvalidate(["toMemberId"=>$toMemberId],["toMemberId"],["req,num"],["会员ID"])){//校验
			return;
		}
		
		$bbsModel = new BbsModel();
		$bbsList = $bbsModel->findMemberFollowBBSList($toMemberId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	
	/**
	 * 会员关注论坛
	 */
	function followBbs()
	{
		$memberId 	= $this->getParam("memberId");
		$bbsId		= $this->getParam("bbsId");
		
		$bbsModel = new BbsModel();
		
		//得到是否已经关注
		$targetBbs = Db::name('t_bbs_follow')->where("memberId",$memberId)->where("bbsId",$bbsId)->find();
		if($targetBbs!=null){
			echo json_encode(array("success"=>0,"code"=>1103,"msg"=>"已经关注了"),true);
			return;
		}
		
		$bbsList = $bbsModel->memberFollowbbs($memberId,$bbsId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsList),"code"=>200,"msg"=>"关注成功"),true);
	}	
	
	
	/**
	 * 会员取消关注论坛
	 */
	function cancelFollowBbs()
	{
		$memberId 	= $this->getParam("memberId");
		$bbsId		= $this->getParam("bbsId");
		
		$bbsModel = new BbsModel();
		
		//得到是否已经关注
		$targetBbs = Db::name('t_bbs_follow')->where("memberId",$memberId)->where("bbsId",$bbsId)->find();
		if($targetBbs==null){
			echo json_encode(array("success"=>0,"code"=>1104,"msg"=>"没有关注此论坛"),true);
			return;
		}
		
		$bbsList = $bbsModel->memberCancelFollowBbs($memberId,$bbsId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsList),"code"=>200,"msg"=>"成功取消关注"),true);
	}	
	
	
	
	
	
	/**
	 * 得到论坛标签
	 */
	function bbsTags()
	{
		$bbsId = $this->getParam("bbsId");
		
		$bbsModel = new BbsModel();
		$bbsTagList = $bbsModel->bbsTags($bbsId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsTagList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	/**
	 * 得到1个论坛详情
	 */
	function bbsDetail()
	{
		$bbsId = $this->getParam("bbsId");
		$memberId = $this->getParam("memberId");
		
		$bbsModel = new BbsModel();
		$bbsTagList = $bbsModel->bbsDetail($bbsId,$memberId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsTagList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	
	//-------------------------------帖子接口----------------------------------------------------------------------
	
	/**
	 * 帖子所有类别
	 */
	function topicCategorys()
	{
		$bbsModel = new BbsModel();
		$bbsCategoryList = $bbsModel->categorysByPid(16);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($bbsCategoryList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	/**
	 * 得到置顶帖
	 */
	function bbsTopTopics()
	{
		$bbsId	= $this->getParam("bbsId");
		$gnum 	= $this->getParam("gn",0);//全局置顶数量
		$bnum 	= $this->getParam("bn",0);//论坛置顶数量
		
		if(!$this->pvalidate(["bbsId"=>$bbsId],["bbsId"],["req,num"],["论坛标识"])){//校验
			return;
		}
		
		$bbsModel = new BbsModel();
		$topList = $bbsModel->bbsTopTopics($bbsId,$gnum,$bnum);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($topList),"code"=>200,"msg"=>"ok"),true);
	}	
	
	/**
	 * 得到帖子列表
	 */
	function bbsTopics()
	{
		$bbsId 	= $this->getParam("bbsId",0);
		$pageNum 	= $this->getParam("pageNum",1);
		$limit	 	= $this->getParam("limit",10);
		
		if(!$this->pvalidate(["bbsId"=>$bbsId],["bbsId"],["req,num"],["论坛标识"])){//校验
			return;
		}
		
		
		$bbsModel = new BbsModel();
		$topicList = $bbsModel->bbsTopics($bbsId,$pageNum,$limit);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($topicList),"code"=>200,"msg"=>"ok"),true);
	}	
	/**
	 * 首页推荐帖子接口 ，根据用户的关注偏好得到的帖子列表
	 */
	function memberTopics()
	{
		$memberId 	= $this->getParam("memberId",0);
		$pageNum 	= $this->getParam("pageNum",1);
		$limit	 	= $this->getParam("limit",10);
		
		$bbsModel = new BbsModel();
		$topList = $bbsModel->memberTopicList($memberId,$pageNum,$limit);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($topList),"code"=>200,"msg"=>"ok"),true);
	}	
//	
	
	/**
	 * 发帖
	 */
	function addTopic()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		//检测是否可以发帖  后期需求
		$title			= $this->getParam("title");
		$bbsId			= $this->getParam("bbsId");
		$avatar			= $this->getParam("avatar");
		$content			= $this->getParam("content");
		$memberId		= $this->getParam("memberId");//memberId是公共参数自动传过来的
		$isReplay		= $this->getParam("isReplay");
		$categoryId		= $this->getParam("categoryId");//
		$imgList			= $this->getParam("imgList");//
		
		$topic = ["title"=>$title,"bbsId"=>$bbsId,"avatar"=>$avatar,"content"=>$content,
		"memberId"=>$memberId,"isReplay"=>$isReplay,"categoryId"=>$categoryId];
		
		if(!$this->pvalidate($topic,["title","bbsId","memberId","isReplay","categoryId","content"],["req","req,num","req,num","req","req,num","req"],
		["标题","论坛标识","用户标识","回复类型","帖子类别","内容"])){//校验
			return;
		}
		
		$bbsModel = new BbsModel();
		$result = $bbsModel->addTopic($topic,$imgList);
		if($result>=1){
			echo json_encode(array("success"=>1,"content"=>_unsetNull($result),"code"=>200,"msg"=>"ok"),true);		
		}else{
			echo json_encode(array("success"=>0,"code"=>-1,"msg"=>"发帖失败"),true);
		}
		
	}	
	
	/**
	 * 发帖
	 */
	function topicDetail()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		
		$memberId	= $this->getParam("memberId");
		$topicId		= $this->getParam("topicId");
		
		if(!$this->pvalidate(["topicId"=>$topicId],["topicId"],["req,num"],["帖子标识"])){//校验
			return;
		}
		
		$bbsModel = new BbsModel();
		$data = $bbsModel->topicDetail($memberId,$topicId);	
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($data),"code"=>200,"msg"=>"ok"),true);
	}
	
}
